<?php

namespace app\services;

use app\lib\exception\ServiceException;
use think\facade\Log;

class PWDBlackService
{
    /**
     * @var mixed
     */
    private $blackIdArr;

    public function __construct($identification)
    {
        $this->blackIdArr = $identification['data'];
    }





    public function curlPost($cmd, $company = "ZUH140")
    {

        if (empty($this->blackIdArr[$company])) throw new ServiceException('黑屏配置错误');
        $postUrl = 'http://8.134.128.125:18119/GetData/soap/get.do';
        $post_data = "cmd=" . urlencode($cmd);
        $post_data .= "&user={$this->blackIdArr[$company]['user']}&pwd={$this->blackIdArr[$company]['pwd']}";//重点
        $ch = curl_init(); // 初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl); // 抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 1); // 设置header
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_POST, 1); // post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $data = curl_exec($ch); // 运行curl
        curl_close($ch);
        preg_match('/RESULT([\S\s]+)\/RESULT/', $data, $result);
        $result = html_entity_decode($result[1], ENT_QUOTES | ENT_HTML5, 'UTF-8');
        Log::channel('black')->info("黑屏记录>{$company}>cmd: {$cmd}>>>>" . $result);

        return $result;
    }
}